#include "dlg_createfile.h"
#include "ui_dlg_createfile.h"
#include "cell_main.h"

#include <QFileDialog>
#include <QFile>
#include <QDebug>

dlg_createFile::dlg_createFile(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::dlg_createFile)
{
    ui->setupUi(this);
}

dlg_createFile::~dlg_createFile()
{
    delete ui;
}

void dlg_createFile::on_btn_confirm_clicked()
{
    //让用户选择新建文件所在的文件夹
    QFileDialog file_dlg;
    file_dlg.setWindowTitle("选择文件夹");
    file_dlg.setFileMode(QFileDialog::Directory);
    file_dlg.exec();
    QString filepath = file_dlg.selectedFiles()[0] + "/";
    QString filename = ui->le_filename->text(); //文件名
    //检查用户输入
    if(!filename.isEmpty()) {
        filepath += filename;
        QFile file(filepath);
        //创建文件
        bool retopen = file.open(QIODevice::WriteOnly);
        if(!retopen) { qDebug() << "open file err"; this->done(0); }
        qDebug() << "open file done";
        cell_main::setcurFileName(filepath);
        file.close();
    }
    this->done(1);
}

void dlg_createFile::on_btn_cancel_clicked()
{
    this->done(2);
}

